package com.co.file.controller;


import com.co.common.entity.Result;
import com.co.common.util.IdWorker;
import com.co.file.service.VedioSaveService;
import com.co.file.util.FastDFSClient;
import com.co.file.util.FastDFSFile;
import com.co.video.pojo.Detail;
import com.co.video.pojo.Video;
import com.co.video.pojo.VideoState;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private VedioSaveService vedioSaveService;

    @Autowired
    private IdWorker idWorker;

    public String url;  //视频保存在fastDFS中的url

    public String aid;  //视频的主键aid

    public String title;//视频的名称信息


    /**
     * 这个功能对应Upload_Video.html
     * 把视频上传到fastDFS并返回url给前端
     * 前端实现把url带给Upload_Edit.html
     *
     * @return
     */
    @PostMapping("/upload")
    public Result<String> uploadFile(MultipartFile file, @RequestParam String username) {
        try {
            //判断文件是否存在
            if (file == null) {
                throw new RuntimeException("文件不存在");
            }

            //获得文件的完整名称
            String originalFilename = file.getOriginalFilename();
            if (StringUtils.isEmpty(originalFilename)) {
                throw new RuntimeException("文件不存在");
            }

            //获得文件的扩展名称
            String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);

            //获取文件内容
            byte[] content = file.getBytes();

            //创建文件上传的封装实体类
            FastDFSFile fastDFSFile = new FastDFSFile(originalFilename, content, extName);

            //基于工具类进行文件的上传，并接收返回的参数
            String[] uploadResult = FastDFSClient.upload(fastDFSFile);

            //封装url
            url = FastDFSClient.getTrackerUrl() + uploadResult[0] + "/" + uploadResult[1];

            //封装title
            title = originalFilename;

            //把当前的数据保存到数据库中
            this.save2Detail(username);
            this.save2Video();
            this.save2State();

            System.out.println(originalFilename);

            //视频到fastDFS中并把url与信息返回给前端
            return Result.ok(url);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.failed("文件上传失败");
    }


    /**
     * 前端传来数据后
     * 把url与aid，username之类的信息保存在tv_vdetail表中
     */
    public void save2Detail(String username) {

        Detail Detail = new Detail();

        this.aid = String.valueOf(idWorker.nextId());

        Detail.setAid(this.aid);

        Detail.setUrl(this.url);

        //把用户的username查出来
        Detail.setUsername(username);

        //title不能为null
        Detail.setTitle(this.title);

        vedioSaveService.save2Detail(Detail);

    }

    /**
     * 把信息保存在Video表中
     */
    public void save2Video() {

        Video video = new Video();

        video.setAid(this.aid);

        video.setCid(String.valueOf(idWorker.nextId()));

        vedioSaveService.save2Video(video);
    }

    /**
     * 把信息保存在state这张表中
     */
    public void save2State() {

        VideoState videoState = new VideoState();

        videoState.setAid(this.aid);

        videoState.setId(String.valueOf(idWorker.nextId()));

        vedioSaveService.save2State(videoState);
    }

    /**
     * 前端传来数据，封装成Detail实体
     * 把这些数据保存在数据库的tv_vdetail中
     *
     * @param Detail
     * @return
     */
    @PostMapping("/edit")
    public Result<String> editFile(@RequestBody Detail Detail) {

        //先把这个视频信息从数据库中查出来
        Detail Detail1 = vedioSaveService.find(this.aid);

        //把前端过来的数据封装到这个实体类当中
        Detail.setTitle(Detail.getTitle());
        Detail.setImage(Detail.getImage());
        Detail1.setComment(Detail.getComment());
        Detail1.setCategoryId(Detail.getCategoryId());

        //把数据保存到数据库中
        vedioSaveService.save2Detail(Detail1);

        //保存url与用户信息到数据库
        return Result.ok(url);

    }
}
